$(function() {

    //date
	var startdate=0;
	var enddate=0;

	var start = {
		elem: '#start',
		format: 'YYYY/MM/DD hh:mm:ss',
	    min: '2013-01-01 00:00:00', //设定最小日期为当前日期
	    max: laydate.now(), //最大日期
	    istime: true,
	    istoday: false,
	    choose: function(datas){
	         end.min = datas; //开始日选好后，重置结束日的最小日期
	         end.start = datas //将结束日的初始值设定为开始日
	         startdate = datas;
		}
	 };
	 var end = {
	 	elem: '#end',
	 	format: 'YYYY/MM/DD hh:mm:ss',
	 	min: '2013-01-01 23:59:59',
	 	max: laydate.now(),
	 	istime: true,
	 	istoday: false,
	 	choose: function(datas){
	        start.max = datas; //结束日选好后，重置开始日的最大日期
	        enddate = datas;
	    }
	};
	laydate(start);
	laydate(end);
	
	$('#date-btn-1').click(function(){
		
		$('.chart-1').empty();
		$('.chart-2').empty();
		$('.chart-3').empty();
		$('tbody').empty();
		
		
		showChart(startdate,enddate,1);
		
		
	});
	
	$('#date-btn-2').click(function(){
		
		$('.chart-1').empty();
		$('.chart-2').empty();
		$('.chart-3').empty();
		$('tbody').empty();
		
		
		showChart(startdate,enddate,2);
		
		
	});
	
	
	$('#date-btn-3').click(function(){
		
		$('.chart-1').empty();
		$('.chart-2').empty();
		$('.chart-3').empty();
		$('tbody').empty();
		
		
		showChart(startdate,enddate,3);
		
		
	});
	
	
	
	
	
	
	
	
	
	$('#myTab').click(function(){
		
		$('.chart-1').empty();
		$('.chart-2').empty();
		$('.chart-3').empty();
		$('tbody').empty();
		
	});
	
	

   

});



function showChart(start,end,flag){
	
	

	
	var data_morris = '[';
	var data_donut = '[';
	var data_bar = '[';
	
	$.getJSON('../../StatsDeal',{
		start:start,
		end:end,
		flag:flag
	},function(data,status){
		
		if(status){
			
			//生成datas数据
			
			var allIn = 0;
			var allOut = 0;
			
			$('tbody').empty();
			
			for(var i = 0; i < data.length; i ++){
				
				var info = data[i];
				
				allIn += parseInt(info.INMONEY);
				allOut += parseInt(info.OUTMONEY);
				
				$('tbody').append('<tr><td>'+(i+1)+'</td><td>'+info.INMONEY+'</td><td>'+info.OUTMONEY+'</td><td>'+info.DATES+'</td></tr>');
				
				if(i == data.length-1){
					data_morris += '{period:\''+info.DATES+'\',ins:'+parseInt(info.OUTMONEY)+',outs:'+info.INMONEY+'}]';
					data_bar += '{y:\''+info.DATES+'\',a:'+parseInt(info.OUTMONEY)+',b:'+info.INMONEY+'}]';
				}else{
					data_morris += '{period:\''+info.DATES+'\',ins:'+parseInt(info.OUTMONEY)+',outs:'+info.INMONEY+'},';
					data_bar += '{y:\''+info.DATES+'\',a:'+parseInt(info.OUTMONEY)+',b:'+info.INMONEY+'},';
				}
		    

				
				
			}
			
			data_donut += '{label: "总收入",value: '+parseInt(allIn)+'}, {label: "总支出",value: '+parseInt(allOut)+'}, {label: "总利润",value: '+(parseInt(allIn)-parseInt(allOut))+' }]';
			
			
			$('.chart-1').append('<div id="morris-area-chart"></div>');
			
			//折线
			var area = Morris.Area({
			    element: 'morris-area-chart',
			    data: [{period:'2015-08-08 12:30:30',ins:1,outs:2}],
			    xkey: 'period',
			    ykeys: ['ins', 'outs'],
			    labels: ['支出', '收入'],
			    pointSize: 2,
			    hideHover: 'auto',
			    resize: true
			});
			
			area.setData(eval(data_morris));
			


			$('.chart-3').append('<div id="morris-donut-chart"></div>');

			//扇形图
			 Morris.Donut({
			        element: 'morris-donut-chart',
			        data: eval(data_donut),
			        resize: true
			    });
			 	
				$('.chart-2').append('<div id="morris-bar-chart"></div>');
				
			 	//柱状图
			    Morris.Bar({
			        element: 'morris-bar-chart',
			        data: eval(data_bar),
			        xkey: 'y',
			        ykeys: ['a', 'b'],
			        labels: ['支出', '收入'],
			        hideHover: 'auto',
			        resize: true
			  });

		}
		
	})
	
	
	
}







